package com.example.dilidili_ting.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.HistoryActivityTing;
import com.example.dilidili_ting.activity.MovieDetailsActivityTing;
import com.example.dilidili_ting.activity.MovielistActivityTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.IOException;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import cn.pedant.SweetAlert.SweetAlertDialog;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HistoryAdapterTing extends BaseAdapter {

    private Activity contextTing;
    private List<MovieBeanTing> dataTing;
    private DataBeanTing<MovieBeanTing> r;

    //    构造函数
    public HistoryAdapterTing(Activity context, List<MovieBeanTing> data) {
        this.contextTing=context;
        this.dataTing=data;
    }

    class VH{
        public LinearLayout llHistoryTing;
        public ImageView ivHistoryImgTing;
        public TextView tvHistoryMovienameTing;
        public TextView tvHistoryCastTing;
        public ImageView ivShanchuTing;
        public RelativeLayout rlScWuTing;
        public ImageView ivXiaoren;
        public TextView tvScWuTing;
    }

    @Override
    public int getCount() {
        return dataTing == null?0:dataTing.size();
    }

    @Override
    public MovieBeanTing getItem(int position) {
        return dataTing == null?null:dataTing.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final VH vh;
        if (convertView==null) {
            vh = new VH();
            convertView = LayoutInflater.from(contextTing).inflate(R.layout.history_list_item,null);
            vh.llHistoryTing = (LinearLayout) convertView.findViewById(R.id.ll_history_ting);
            vh.ivHistoryImgTing = (ImageView) convertView.findViewById(R.id.iv_history_img_ting);
            vh.tvHistoryMovienameTing = (TextView) convertView.findViewById(R.id.tv_history_moviename_ting);
            vh.tvHistoryCastTing = (TextView) convertView.findViewById(R.id.tv_history_cast_ting);
            vh.ivShanchuTing = (ImageView) convertView.findViewById(R.id.iv_shanchu_ting);
            vh.rlScWuTing = (RelativeLayout) convertView.findViewById(R.id.rl_sc_wu_ting);
            vh.ivXiaoren = (ImageView) convertView.findViewById(R.id.iv_xiaoren);
            vh.tvScWuTing = (TextView) convertView.findViewById(R.id.tv_sc_wu_ting);
            convertView.setTag(vh);
        }else {
            vh= (VH) convertView.getTag();
        }
        final MovieBeanTing  historylist = getItem(position);
        if(historylist != null){
            vh.rlScWuTing.setVisibility(View.GONE);
            //加载网络图片
            RequestOptions options = new RequestOptions().error(R.drawable.k8).bitmapTransform(new RoundedCorners( 10 ));//图片圆角为10
            Glide.with(vh.ivHistoryImgTing.getContext()).load(dataTing.get(position).getMovieImage()).apply(options).into(vh.ivHistoryImgTing);
//            System.out.println("hhhhhhhhhhhhh");
            vh.tvHistoryMovienameTing.setText(dataTing.get(position).movieName);
            vh.tvHistoryCastTing.setText(dataTing.get(position).cast);
//            跳转到详情页
            vh.llHistoryTing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2021/6/17 调接口传入信息,获取电影详情,并跳转页面
                    Intent intent = new Intent(contextTing, MovieDetailsActivityTing.class);
                    intent.putExtra("movieName", dataTing.get(position).movieName);
                    contextTing.startActivity(intent);
                }
            });

//            删除播放记录
            vh.ivShanchuTing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //删除弹窗
                        new SweetAlertDialog(contextTing, SweetAlertDialog.WARNING_TYPE)
                            .setTitleText("你确定要删除吗")
                            .setConfirmText("确定")
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    sDialog
                                            .setTitleText("删除成功")
                                            .setConfirmText("OK")
                                            .setConfirmClickListener(null)
                                            .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);

                                            contextTing.runOnUiThread(new Runnable(){
                                                @Override
                                                public void run() {
                                                    Timer timer=new Timer();
                                                    TimerTask task=new TimerTask() {
                                                        @Override
                                                        public void run() {
                                                            click(dataTing.get(position).movieId,vh);
                                                            dataTing.remove(dataTing.get(position));
                                                            sDialog.dismiss();
                                                        }
                                                    };
                                                    timer.schedule(task,1200);
                                                }
                                            });
                                        }
                                     })
                                    .show();
                }
            });
        }
        return convertView;
    }

    private void click(int movieId, VH vh) {
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserID(contextTing));
        jo.addProperty("movieId",movieId);
        OkhttpUtilsTing.isPOST("playHistory/removePlayHistory", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                contextTing.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        HistoryAdapterTing.this.notifyDataSetChanged();
                    }
                });
            }
        });
    }
}
